package com.hellobike.haxing.modules.wms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.modules.wms.dto.ExportStockBatchLocDto;
import com.hellobike.haxing.modules.wms.dto.StockBatchLocDto;
import com.hellobike.haxing.modules.wms.entity.StockBatchLoc;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: t_wms_stock_batch_loc
 * @Author: haxing-boot
 * @Date: 2020-01-09
 * @Version: V1.0
 */
public interface StockBatchLocMapper extends BaseMapper<StockBatchLoc> {

    @Update("update t_wms_stock_batch_loc set available_qty = available_qty + #{qty},update_time=now() where deleted=0 AND id=#{id}")
    int addStock(@Param("id") Long id, @Param("qty") BigDecimal qty);

    @Update("update t_wms_stock_batch_loc set available_qty = available_qty - #{qty},update_time=now() where deleted=0 AND id=#{id} and available_qty - #{qty}>=0")
    int subStock(@Param("id") Long id, @Param("qty") BigDecimal qty);

    @Select("<script>" +
            "SELECT s.*,sp.spu_name,sp.store_type,sk.sku_code,sk.barcode,l.loc_code,b.batch_no,b.production_date,sk.dimension_values,p.specifications FROM t_wms_stock_batch_loc as s " +
            "LEFT JOIN t_wms_location l ON l.id=s.loc_id " +
            "LEFT JOIN t_sku sk ON sk.id=s.sku_id " +
            "LEFT JOIN t_spu sp on sp.id=sk.spu_id " +
            "LEFT JOIN t_product p on  p.id=sk.spu_id " +
            "LEFT JOIN t_wms_stock_batch_att as b on b.id=s.batch_id " +
            "WHERE s.deleted=0 AND s.warehouse_id=#{p.warehouseId} " +
            "<if test='p.barcode != null and p.barcode != \"\"'>" +
            "AND sk.barcode LIKE '%${p.barcode}%' " +
            "</if>" +
            "<if test='p.spuName != null and p.spuName != \"\"'>" +
            "AND sp.spu_name LIKE '%${p.spuName}%'" +
            "</if>" +
            " order by s.id desc " +
            "</script>")
    IPage<StockBatchLocDto> pageByStockBatchLocDto(Page<StockBatchLocDto> page, @Param("p") StockBatchLocDto stockBatchLoc);

    @Select("<script>" +
            "SELECT s.*,sp.spu_name,sp.store_type,sk.sku_code,sk.barcode,l.loc_code,b.batch_no,b.production_date,sk.dimension_valuesFROM t_wms_stock_batch_loc as s " +
            "LEFT JOIN t_wms_location l ON l.id=s.loc_id " +
            "LEFT JOIN t_sku sk ON sk.id=s.sku_id " +
            "LEFT JOIN t_spu sp on sp.id=sk.spu_id " +
            "LEFT JOIN t_wms_stock_batch_att as b on b.id=s.batch_id " +
            "WHERE s.deleted=0 AND s.warehouse_id=#{p.warehouseId} " +
            "<if test='p.barcode != null and p.barcode != \"\"'>" +
            "AND sk.barcode LIKE '%${p.barcode}%' " +
            "</if>" +
            "<if test='p.spuName != null and p.spuName != \"\"'>" +
            "AND sp.spu_name LIKE '%${p.spuName}%'" +
            "</if>" +
            " order by s.id desc " +
            "</script>")
    List<ExportStockBatchLocDto> exportStockBatchLoc(@Param("p") StockBatchLocDto stockBatchLoc);
}
